home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 250 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.6 KB

  1. Path: a3.complang.tuwien.ac.at!schwarz
  2. From: schwarz@a3.complang.tuwien.ac.at (Konrad Schwarz)
  3. Newsgroups: comp.std.c
  4. Subject: Re: integral types in switch expressions
  5. Date: 31 Jan 1996 10:42:03 GMT
  6. Organization: TU Wien
  7. Message-ID: <4enh1r$dq7@news.tuwien.ac.at>
  8. References: <4eb5r1$b04@news.tuwien.ac.at> <1996Jan26.220546.22346@sq.com>
  9. NNTP-Posting-Host: a3.complang.tuwien.ac.at
  10.  
  11. In article <1996Jan26.220546.22346@sq.com>, msb@sq.com (Mark Brader) writes:
  12. |> Konrad Schwarz (schwarz@mips.complang.tuwien.ac.at) writes:
  13. |> > T a [] = { ... } ...
  14.  
  15. [ ... ]
  16.  
  17. |> I don't know what was in the Committee's minds, but I can point out
  18. |> that this code contains constant pointer expressions only when a[]
  19. |> has static storage duration. 
  20.  
  21. Sorry about being unclear about this.  I did not know that ANSI C allows
  22. initialization of any automatic object.  I meant ``a'' to have static
  23. storage duration and thought this was clear from the context.
  24.  
  25. |> Therefore, without extending the switch
  26. |> statement to support case-expressions that would have to be computed
  27. |> at run time, you would have a construct that would be useful for some
  28. |> pointer values but not others.
  29.  
  30. just as only certain expressions are allowed in case expressions
  31.  
  32. |> 
  33. |> > I am of course aware that
  34. |> > switch (c - a) {
  35. |> >     case 0: ...
  36. |> >     case 1: ...
  37. |> >     case 3: ...
  38. |> >     default: ...
  39. |> > }
  40. |> > is equivalent code, but it suffers from the division needed to evaluate
  41. |> > c - a.
  42. |> 
  43. |> No, unfortunately, it isn't.  The first one, if it worked at all, would
  44. |> work for *any* value c of appropriate type.  In the second one, c must
  45. |> point to an element of a[], or one past the last element.  But the
  46. |> overhead of division, at least, is a red herring given a suitable
  47. |> optimizer, as the code can simply be compiled as if it read:
  48. |> 
  49. |>   switch ((char *) c - (char *) a) {
  50. |>       case 0 * sizeof *a: ...
  51. |>       case 1 * sizeof *a: ...
  52. |>       case 3 * sizeof *a: ...
  53. |>       default: ...
  54. |>   }
  55.  
  56. Given a suitable optimizer, any language deficiency can be optimized away.
  57. I believe languages should not
  58. introduce gratuitous restrictions in the first place.
  59.  
  60. The illegal code mentioned in my original posting would have useful to me.
  61. I find its illegality arbitrary.  The non-equivalence between the first
  62. and second version of my code you point out only exacerbates the situation,
  63. since a more general version of the code is:
  64.  
  65. static T a, b;
  66. T *c;
  67. ...
  68. switch (c) {
  69.     case &a: ...
  70.     case &b: ...
  71.     default: ...
  72. }
  73.  
  74. for which no translation is possible, except as a series of ``if'' statements
  75. of the kind the ``switch'' statement is presumably designed to avoid.
  76.  
  77. Konrad Schwarz
  78.